package com.ruibang.glass.portal.controller;

import com.alibaba.fastjson.JSON;
import com.ruibang.glass.portal.domain.RoleMenuReq;
import com.ruibang.glass.portal.domain.RolePageParams;
import com.ruibang.glass.portal.domain.RoleParams;
import com.ruibang.glass.portal.entity.RoleEntity;
import com.ruibang.glass.portal.service.RoleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/role")
@Slf4j
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation( value = "保存角色信息", response = ResultBody.class)
    @PostMapping(value = "/saveRole")
    public ResultBody<Void> saveRole(@RequestBody @Valid RoleParams params) {
        return roleService.saveRole(params);
    }

    @ApiOperation( value = "修改角色信息", response = ResultBody.class)
    @PostMapping(value = "/updateRole")
    public ResultBody<Void> updateRole(@RequestBody @Valid RoleParams params) {
        return roleService.update(params);
    }

    @ApiOperation( value = "分页获取角色", response = ResultBody.class)
    @PostMapping(value = "/listRole")
    public ResultBody<PageResult<List<RoleEntity>>> getRoleList(@RequestBody RolePageParams roleEntity) {
        return roleService.getRoleList(roleEntity);
    }

    @ApiOperation( value = "获取所有角色", response = ResultBody.class)
    @PostMapping(value = "/queryAllRoles")
    public ResultBody<List<RoleEntity>> queryAllRoles(@RequestBody RolePageParams roleEntity) {
        return ResultBody.success(roleService.queryAllRoles(roleEntity));
    }

    @ApiOperation( value = "删除角色", response = ResultBody.class)
    @PostMapping(value = "/deleteRole/{id}")
    public ResultBody<Void> getMenuList(@PathVariable(value = "id") String roleId) {
        return roleService.remove(roleId);
    }

    @ApiOperation( value = "更新角色用户信息", response = ResultBody.class)
    @PostMapping(value = "/modifyRoleMenu")
    public ResultBody modifyRoleMenu(@RequestBody RoleParams params) {
        log.info("更新角色菜单信息：{}", JSON.toJSONString(params));
        return roleService.modifyRoleMenu(params);
    }

    @ApiOperation( value = "获取用户对应角色", response = ResultBody.class)
    @GetMapping(value = "/getRolesByUserId/{userId}")
    public ResultBody getRolesByUserId(@PathVariable(value = "userId") String userId) {
        return roleService.getRolesByUserId(userId);
    }

    @ApiOperation( value = "根据角色Ids获取用户Ids", response = ResultBody.class)
    @PostMapping(value = "/getUserIdsByRoleIds")
    public ResultBody getUserIdsByRoleIds(@RequestBody List<String> roleIds) {
        return ResultBody.success(roleService.getUserIdsByRoleIds(roleIds));
    }

    @ApiOperation( value = "根据菜单ID获取角色信息", response = ResultBody.class)
    @GetMapping(value = "/getRoleByMenuId/{menuId}")
    public ResultBody getRoleByMenuId(@PathVariable String menuId) {
        return ResultBody.success(roleService.getRoleByMenuId(menuId));
    }

    @ApiOperation( value = "修改角色菜单", response = ResultBody.class)
    @PostMapping(value = "/editRoleMenu")
    public ResultBody editRoleMenu(@RequestBody RoleMenuReq roleMenuReq) {
        roleService.editRoleMenu(roleMenuReq);
        return ResultBody.success();
    }
}
